﻿using System.Xml;

namespace Jayden.Dll.Sc2.Descriptions.Terran
{
	public class BuildAddOnActionDescription : BuildActionDescription
	{
		private string m_BuildingId;
		private BuildingWithAddOnDescription m_Building;
		private string m_AddOnId;
		private AddOnAttachedDescription m_AddOn;

		public BuildAddOnActionDescription(EntityDescription entity, XmlNode node)
			: base(entity, node)
		{
			m_BuildingId = node.Attributes["building"].Value;
			m_AddOnId = node.Attributes["addon"].Value;
		}

		public BuildingWithAddOnDescription Building
		{
			get
			{
				if (m_Building == null && m_BuildingId != null)
					m_Building = Entity.Race[m_BuildingId] as BuildingWithAddOnDescription;
				return m_Building;
			}
		}

		public AddOnAttachedDescription AddOn
		{
			get
			{
				if (m_AddOn == null && m_AddOnId != null)
					m_AddOn = Entity.Race[m_AddOnId] as AddOnAttachedDescription;
				return m_AddOn;
			}
		}

		public override void Execute(GameState state)
		{
			RemoveResources(state);
			state.Add(Entity, 0, -1, -1);
			state.Add(Building, 1, 0, 1);
			state.Add(AddOn, 1, 0, 1);
			BaseExecute(state);
			state.Add(new DelayedActionDescription(this, Build.Time + state.Time, delegate(GameState innerState)
			{
				innerState.Add(Building, -1, 1, 0);
				innerState.Add(AddOn, -1, 1, 0);
			}));
		}
	}
}
